package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.annotation.AddLog;
import com.baizhi.annotation.DelCache;
import com.baizhi.dao.AdminDAO;
import com.baizhi.entity.Admin;
import com.baizhi.entity.AdminExample;
import com.baizhi.entity.User;
import com.baizhi.entity.UserExample;
import com.baizhi.service.AdminService;
import com.baizhi.util.UUIDUtil;
import com.baizhi.vo.CommonVO;
import org.apache.catalina.core.StandardContext;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Transactional
@Service
public class AdminServiceImpl implements AdminService {


    private static final Logger log = LoggerFactory.getLogger(AdminServiceImpl.class);
    @Resource
    AdminDAO adminDAO;

    @Resource
    HttpServletRequest request;

    @Override
    public HashMap<String, Object> login(Admin admin, String enCode) {

        //1.获取验证码
        String code = (String) request.getServletContext().getAttribute("code");

        log.info("用户输入的验证码：{}",enCode);
        log.info("获取存储的验证码：{}",code);

        HashMap<String, Object> map = new HashMap<>();

        //2.判断验证码是否一致
        if(code.equals(enCode)){
            //3.判断用户是否存在
            Admin admins = adminDAO.queryByUsername(admin.getUsername());

            if(admins!=null){
                //4.判断用户状态是否正常
                if(admins.getStatus().equals("1")){

                    //5.判断密码是否正确
                    if(admin.getPassword().equals(admins.getPassword())){

                        //存储用户标记
                        request.getServletContext().setAttribute("admin",admins);

                        map.put("message",admins);
                        map.put("status",200);
                    }else{
                        map.put("message","密码错误");
                        map.put("status",400);
                    }
                }else{
                    map.put("message","该用户已被冻结请联系超级管理员");
                    map.put("status",400);
                }
            }else{
                map.put("message","该用户不存在");
                map.put("status",400);
            }
        }else{
            map.put("message","验证码不正确");
            map.put("status",400);
        }
        return map;
    }

    @AddCache
    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {

        HashMap<String, Object> map = new HashMap<>();

        //设置当前页
        map.put("page",page);

        //设置查询条件对象
        AdminExample example = new AdminExample();

        //查询总条数
        int count = adminDAO.selectCountByExample(example);

        //设置总条数
        map.put("total",count);

        //创建分页对象  参数：起始条数,数据数
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);

        //根据分页查询数据
        List<Admin> admins = adminDAO.selectByExampleAndRowBounds(example, rowBounds);

        //设置数据
        map.put("rows",admins);

        return map;
    }

    @DelCache
    @AddLog(value = "修改管理员信息")
    @Override
    public HashMap<String, Object> update(Admin admin) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            adminDAO.updateByPrimaryKeySelective(admin);
            map.put("message","修改成功!!!!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败!!!!");
        }
        return map;
    }

    @DelCache
    @AddLog(value = "删除管理员")
    @Override
    public HashMap<String, Object> delete(Admin admin) {
        HashMap<String, Object> map = new HashMap<>();
        //删除数据
        try {
            adminDAO.delete(admin);

            map.put("message","数据删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","数据删除失败");
        }
        return map;
    }

    @DelCache
    @AddLog(value = "添加管理员")
    @Override
    public CommonVO add(Admin admin) {

        try {
            admin.setId(UUIDUtil.getUUID());
            admin.setStatus("1");

            adminDAO.insertSelective(admin);
            return CommonVO.success("管理员添加成功！！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild("管理员添加失败！！！！");
        }
    }

    @Override
    public Admin queryById(String id) {
        return adminDAO.selectByPrimaryKey(id);
    }
}
